<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class ScoreController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name = input('name');
        $score = Db::name('score')
            ->where(function ( Query $query) use ($name){
                $query->where('name','like',"%$name%");
            })
            ->order('id DESC')
            ->paginate('3',false,['query'=>$this->request->param()]);
        $page=$score->render();
        return $this->fetch('',['score'=>$score,'page'=>$page]);
    }

    /**
     * 显示添加资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function add()
    {
        return $this->fetch('score_add');
    }
    public function addPost()
    {
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('积分不能为空');
            }
            $add = Db::name('score')->insert(input());
            if($add)
                $this->success('添加成功','score/index');
                $this->error('添加失败');
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $score = Db::name('score')->get($id);
        return $this->fetch('score_edit',['score'=>$score]);
    }
    public function editPost()
    {
        if(request()->isPost()){
            if(empty(input('name'))){
                $this->error('积分不能为空');
            }
            $edit = Db::name('score')->update(input());
            if($edit)
                $this->success('修改成功','score/index');
            $this->error('修改失败');
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $del =Db::name('score')->delete($id);
        if($del)
            $this->success('删除成功','score/index');
        $this->error('删除失败');
    }
}
